﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

using LIM.model;

namespace LIM.controller
{
    public class SYSController : BaseController
    {
        public SYSController() { }
        public SYSController(LIMS_Entities LIMS_Entities)
        {
            this.LIMS_Entities = LIMS_Entities;
        }
        public virtual void Refresh_tbl_DanhMuc_ThongTinNhanVien()
        {
            LIMS_Entities.Refresh(RefreshMode.StoreWins, LIMS_Entities.tbl_DanhMuc_ThongTinNhanVien);
        }
        public virtual void Refresh_tbl_Users_Roles()
        {
            LIMS_Entities.Refresh(RefreshMode.StoreWins, LIMS_Entities.tbl_SYS_Roles);
            LIMS_Entities.Refresh(RefreshMode.StoreWins, LIMS_Entities.tbl_SYS_Users);
        }
        public List<tbl_DanhMuc_ThongTinNhanVien> LayDanhSach_ThongTinNhanVien()
        {
            var results = LIMS_Entities.tbl_DanhMuc_ThongTinNhanVien.ToList();
            return results;
        }
        public List<tbl_SYS_Roles> GetList_Roles()
        {
            var results = LIMS_Entities.tbl_SYS_Roles.ToList();
            return results;
        }
        public List<tbl_SYS_Roles> GetList_UnassignedRoles(int RowIDUser)
        {
            var results = LIMS_Entities.tbl_SYS_Roles.Where(r => !r.tbl_SYS_Users.Where(u => u.RowID == RowIDUser).Any()).ToList();            
            return results;
        }
        public List<tbl_SYS_Roles> GetList_AssignedRoles(int RowIDUser)
        {
            var results = LIMS_Entities.tbl_SYS_Roles.Where(r => r.tbl_SYS_Users.Where(u => u.RowID == RowIDUser).Any()).ToList();            
            return results;
        }
        public void Add_UserInRole(tbl_SYS_Users user, tbl_SYS_Roles role)
        {
            user.tbl_SYS_Roles.Add(role);
            LIMS_Entities.SaveChanges();

            Refresh_tbl_Users_Roles();
        }
        public void Remove_UserInRole(tbl_SYS_Users user, tbl_SYS_Roles role)
        {
            user.tbl_SYS_Roles.Remove(role);
            LIMS_Entities.SaveChanges();

            Refresh_tbl_Users_Roles();
        }
        public List<tbl_SYS_MenuPermissions> GetList_MenuPermissions_ByRowIDRole(int rowIDRole)
        {
            var results = LIMS_Entities.tbl_SYS_MenuPermissions.Where(m => m.RowIDRole == rowIDRole).ToList();
            return results;
        }
        public void Update_MenuPermissions_ByRowIDRole(int rowIDRole, List<MenuPermissionsInfo> menuPermissionsInfos)
        {
            foreach (var item in GetList_MenuPermissions_ByRowIDRole(rowIDRole))
            {
                LIMS_Entities.DeleteObject(item);
            }

            foreach (var item in menuPermissionsInfos)
            {
                if (item.IsVisibility)
                {
                    LIMS_Entities.tbl_SYS_MenuPermissions.AddObject(new tbl_SYS_MenuPermissions
                {
                        RowIDRole = rowIDRole,
                        MenuName = item.Name,
                        IsVisibility = true,
                        CreatedDate = GetCurrentDateTimeFromDatabaseServer(),
                        CreatedBy = InitUserInfos.UserName,
                        ModifiedDate = GetCurrentDateTimeFromDatabaseServer(),
                        ModifiedBy = InitUserInfos.UserName
                    });
                }
            }

            LIMS_Entities.SaveChanges();

            LIMS_Entities.Refresh(RefreshMode.StoreWins, LIMS_Entities.tbl_SYS_MenuPermissions);
        }
        public List<tbl_SYS_FormPermissions> GetList_FormPermissions_ByRowIDRole(int rowIDRole)
        {
            var results = LIMS_Entities.tbl_SYS_FormPermissions.Where(m => m.RowIDRole == rowIDRole).ToList();
            return results;
        }
        public void Update_FormPermissions_ByRowIDRole(int rowIDRole, List<FormPermissionsInfo> formPermissionsInfos)
        {
            List<ControlPermissionsInfo> controlPermissionsInfos =new List<ControlPermissionsInfo>();

            foreach (var item in GetList_FormPermissions_ByRowIDRole(rowIDRole))
            {
                LIMS_Entities.DeleteObject(item);
            }

            foreach (var item in GetList_ControlPermissions_ByRowIDRole(rowIDRole))
            {
                LIMS_Entities.DeleteObject(item);
            }

            foreach (var item in formPermissionsInfos)
            {
                if (item.IsVisibility)
                {
                    LIMS_Entities.tbl_SYS_FormPermissions.AddObject(new tbl_SYS_FormPermissions
                    {
                        RowIDRole = rowIDRole,
                        FormName = item.Name,
                        IsVisibility = true,
                        CreatedDate = GetCurrentDateTimeFromDatabaseServer(),
                        CreatedBy = InitUserInfos.UserName,
                        ModifiedDate = GetCurrentDateTimeFromDatabaseServer(),
                        ModifiedBy = InitUserInfos.UserName
                    });


                    foreach (var item2 in item.Controls)
                    {
                        if (item2.IsVisibility)
                        {
                            LIMS_Entities.tbl_SYS_ControlPermissions.AddObject(new tbl_SYS_ControlPermissions
                            {
                                RowIDRole = rowIDRole,
                                FormName = item.Name,
                                ControlName = item2.Name,
                                IsVisibility = true,
                                IsEnabled = item2.IsEnabled,
                                CreatedDate = GetCurrentDateTimeFromDatabaseServer(),
                                CreatedBy = InitUserInfos.UserName,
                                ModifiedDate = GetCurrentDateTimeFromDatabaseServer(),
                                ModifiedBy = InitUserInfos.UserName
                            });
                        }
                    }
                }
            }
            
            LIMS_Entities.SaveChanges();

            LIMS_Entities.Refresh(RefreshMode.StoreWins, LIMS_Entities.tbl_SYS_FormPermissions);
            LIMS_Entities.Refresh(RefreshMode.StoreWins, LIMS_Entities.tbl_SYS_ControlPermissions);
        }

        public List<tbl_SYS_ControlPermissions> GetList_ControlPermissions_ByRowIDRole(int rowIDRole)
        {
            var results = LIMS_Entities.tbl_SYS_ControlPermissions.Where(m => m.RowIDRole == rowIDRole).ToList();
            return results;
        }
        public tbl_SYS_Users Get_Users_ByUsernamePassword(string username, string password)
        {
            var results = LIMS_Entities.tbl_SYS_Users.Where(u => u.UserName == username && u.Password == password).FirstOrDefault();
            return results;
        }
    }
}
